import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sports_new/widgets/choice_bar.dart';
import '../../controller/record/channel_logic.dart';
import '../../entity/record/channel_entity.dart';
import '../../resources/colours.dart';
import '../../resources/styles.dart';
import '../../utils/utils.dart';
import '../../widgets/base/qkd_text.dart';

class ChannelPage extends StatefulWidget {
  const ChannelPage({super.key, this.data, this.type});

  final ChannelEntity? data;
  final int? type;

  @override
  State<ChannelPage> createState() => _ChannelPageState();
}

class _ChannelPageState extends State<ChannelPage>{
  late ChannelLogic logic = Get.put(ChannelLogic(),tag: "${widget.data?.channelId}${widget.type ?? ""}");

  @override
  Widget build(BuildContext context) {
    return GetBuilder<ChannelLogic>(
      tag: "${widget.data?.channelId}${widget.type ?? ""}",
      initState: (state){
        logic.recordType = widget.type ?? 0;
        logic.tag = "${widget.data?.channelId}${widget.type ?? ""}";
        logic.getData(widget.data);
      },
      builder: (logic) {
        return SizedBox(
          height: Get.height,
          child: Column(
            children: [
              pageChoice(),
              Expanded(
                child: PageView(
                  physics: const NeverScrollableScrollPhysics(),
                  controller: logic.pageController,
                  onPageChanged: (index) => logic.typeIndex = index,
                  children: logic.page,
                ))
            ]),
        );
      }
    );
  }

  Widget pageChoice(){
    return Container(
      decoration: const BoxDecoration(
        border: Border(bottom: BorderSide(width: 0.5,color: Colours.greyEE))),
      padding: const EdgeInsets.only(left: 12,right:8,top: 5,bottom: 5),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          Expanded(
            child: ChoiceBar(
              initialIndex: logic.seasonIndex,
              items: logic.typeList,
              onTap: (index) {
                Utils.onEvent(
                  "sjpd_lx",params: {"sjpd_lx":logic.umengList.indexOf(logic.typeList[index])});
                logic.pageController.jumpToPage(index);
              })
          ),
          Container(width: 6),
          GestureDetector(
            onTap: () {
              Utils.onEvent("sjpd_lx",params: {"sjpd_lx":0});
              logic.showDatePicker();},
            child: Container(
              // key: ValueKey(widget.data?.channelId),
              height: 20,
              alignment: Alignment.center,
              padding: const EdgeInsets.symmetric(horizontal: 5),
              decoration: BoxDecoration(
                border: Border.all(color: Colours.grey99,width: 0.5),
                borderRadius: BorderRadius.circular(15)),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  QkdText(logic.season.length > 4?
                  ("${logic.season.substring(2,4)}/${logic.season.substring(7,9)}")
                    :logic.season,
                    needCenter: true,
                    fontSize: 12
                  ),
                  const SizedBox(width: 2),
                  Padding(
                    padding: const EdgeInsets.only(top: 1),
                    child: Image.asset(width: 6,Utils.getImgPath('down_arrow.png')))
                ]),
            ),
          )
        ],
      ),
    );
  }
}
